from Spider.Base import Base
import re
"""
必应图片采集PoiImage
https://cn.bing.com/images

"""
class BingImageSpider(Base):
    def __init__(self,logger,proxy_radio):
        super(BingImageSpider, self).__init__(logger, proxy_radio)
        self.name = "BingImage"
        self.ch_name = "必应图片"
        self.platform_id = 0

    def __searchImageArrayWithPage(self, keyword, page=1):
        state = False
        imageArray = []

        first = 1+ (page-1) * 35

        urlParams = [
            ("q",keyword),
            ("first", str(first)),
            ("count", "35"),
            # ("cw","1436"),
            # ("ch","150"),
            ("relp","1"),
            ("tsc","ImageBasicHover"),
            ("datsrc","I"),
            ("layout","RowBased_Landscape"),
            ("mmasync","1"),
            # ("dgState","x*1162_y*1134_h*180_c*4_i*36_r*7"),
            ("IG","96BA96E090AD44BABFDE41800AB5FF9B"),
            ("SFX","2"),
            ("iid","images.5561"),

        ]

        url = "https://cn.bing.com/images/async"

        index = 0
        for k,v in urlParams:
            if 0==index:
                url+="?%s=%s"%(k,v)
            else:
                url+="&%s=%s" % (k, v)
            index+=1

        headers = {
            "Content-Type": "text/html; charset=UTF-8"
        }
        res = self.get(self.getCurrentFuncName(),url,headers)
        if res:
            content_len = len(res.text)
            if content_len > 1000:
                try:
                    a_array = re.findall('(<a.*?>.*?</a>)', res.text, re.I | re.S | re.M)
                    # print(len(a_array))
                    for a_text in a_array:
                        a_m = re.findall('m=\"{(.*?)}\"',a_text, re.I | re.S | re.M)
                        item = {}
                        if len(a_m) > 0:
                            a_m = a_m[0]
                            a_m = a_m.replace("&quot;","")
                            a_m_array = a_m.split(",")
                            for a_m_kv in a_m_array:
                                if a_m_kv.startswith("t:"):
                                    item["name"] = a_m_kv[2:].replace("\ue000","").replace("\ue001","-")
                                elif a_m_kv.startswith("murl:"):
                                    item["url_source"] = a_m_kv[5:]
                                elif a_m_kv.startswith("purl:"):
                                    item["url_source_org"] = a_m_kv[5:]

                        if item.get("url_source"):
                            imageArray.append(item)

                    if len(imageArray) > 0:
                        state = True
                    else:
                        self.logger.error("keyword=%s,page=%d,imageArray=0|%s" % (keyword, page, url))

                except Exception as e:
                    self.logger.error("keyword=%s,page=%d,%s|%s" % (keyword, page, str(e), url))
            else:
                self.logger.error("keyword=%s,page=%d,content_len=%d|%s" % (keyword, page, content_len, url))
        return state,imageArray
    def searchImageArray(self,keyword,pageCount):
        """

        :param keyword:
        :param pageCount:
        :return:
        [
            {'url_source_org': 'https://news.sina.com.cn/c/2020-11-22/doc-iiznctke2670064.shtml?tr=214', 'url_source': 'http://n.sinaimg.cn/spider20201122/290/w1080h810/20201122/74e0-kefmphc4278308.jpg', 'name': '紧急扩散！唐山-多家医院发布重要消息！|新冠肺炎|唐山-|河北 ...'}
            {'url_source_org': 'http://www.chinabuildingcentre.com/show-6-640-1.html', 'url_source': 'http://www.chinabuildingcentre.com/uploadfile/2013/0131/20130131031715973.jpg', 'name': '唐山市-城市展览馆 - 项目 - 中国建筑中心-官网'}
        ]

        """
        s = set()
        effective_count = 0
        repeat_count = 0
        imageArray = []

        for page in range(1, pageCount+1):
            _state, _imageArray = self.__searchImageArrayWithPage(keyword=keyword, page=page)
            if _state:
                for d in _imageArray:
                    if d.get("url_source") not in s:
                        effective_count +=1
                        imageArray.append(d)
                        s.add(d.get("url_source"))
                    else:
                        repeat_count += 1
            else:
                break
        s.clear()

        # self.logger.info("keyword=%s,pageCount=%d,effective_count=%d,repeat_count=%d"%(keyword,pageCount,effective_count,repeat_count))

        return imageArray

if __name__ == '__main__':
    import logging
    logger = logging.getLogger()
    imageSpider = BingImageSpider(logger, -1)
    imageArray = imageSpider.searchImageArray(keyword="唐山市唐山体育中心", pageCount=3)

    print("共计获取：",len(imageArray))
    for d in imageArray:
        print(d)




